import { defaultBookState, defaultUserState, defaultAsideState, defaultMessageState,
         defaultBookState_RAW, defaultUserState_RAW } from './defaultState';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { regulateBookSearchResult, logOut, isLike } from './middleware';
import { fromJS, Map } from 'immutable';


function BOOK( state = defaultBookState, action ){
    switch( action.type ){
        case 'BY_KEYWORD':
            return state.set( 'render', fromJS( action.searchResult ) )
                        .setIn( ['unrender', 'keyword'], action.searchResult.keywords);

        case 'BY_NEXT_PAGE':
            return state.set( 'render', fromJS( action.searchResult ) );

        case 'CLEAR_ALL_BOOK':
            return defaultBookState_RAW

        case 'REMOVE_OFFSET':
            return state.setIn( ['unrender', 'offset'], undefined );

        default:
            return state;
    }
}

function USER( state = defaultUserState, action ){
    switch( action.type ){
        case 'LOGIN':
            return state.mergeDeep(Map({
                render: fromJS( action.loginResult ),
                unrender: Map({
                    isLogin: true
                })
            }))

        case 'LOGOUT':
            return defaultUserState_RAW.updateIn( ['unrender', 'isSpread'], () => true );

        case 'RENEW_BOOK':
            return state.updateIn( ['render', 'book' ], list => {
                return list.map( book => {
                    if( book.get( 'bookid' ) === action.bookid ){
                        return book.merge({
                            renew: action.renew,
                            time: action.time
                        })
                    }
                    return book;
                })
            });

        case 'ADD_FEED_BOOK':
            return state.updateIn(['render', 'subscibe'], function(k){
                return k.push( Map(action.book) );
            });

        case 'REMOVE_FEED_BOOK':
            return state.updateIn(['render', 'subscibe'], function(k){
                return k.delete(k.findIndex(function(e){
                    return e.get('ctrlid') === action.ctrlid;
                }));
            });

        case 'SPREAD_USER_PANEL':
            return state.updateIn( [ 'unrender', 'isSpread'], is => !is );

        default:
            return state;
    }
}

function ASIDE( state = defaultAsideState, action ){
    switch( action.type ){
        case 'SHOW_ASIDE':
            return state.setIn( [ 'unrender', 'isSpread' ], true )
                        .setIn( [ 'render', 'isLike' ], action.isLike )
                        .setIn( [ 'render', 'book' ], fromJS( action.book ) );

        case 'HIDE_ASIDE':
            return state.setIn( [ 'unrender', 'isSpread' ], false );

        // 当喜欢按钮点击时，同时更新aside的状态
        case 'ADD_FEED_BOOK':
            return state.setIn( [ 'render', 'isLike' ], true );

        // 当取消喜欢按钮点击时，同时更新aside的状态
        case 'REMOVE_FEED_BOOK':
            return state.setIn( [ 'render', 'isLike' ], false );

        case 'GET_DOUBAN':
            return state.setIn( [ 'render', 'douban' ], Map({
                isbn: action.isbn,
                summary: action.summary,
                link: action.link
            }));

        default:
            return state;
    }
}

function MESSAGE( state = defaultMessageState, action ){
    switch( action.type ){
        case 'SHOW_MESSAGE':
            return state.set( 'isShow', true )
                        .set( 'text', action.text )
                        .set( 'action', action.type )

        case 'HIDE_BOX':
            return state.set( 'isShow', false )
                        .set( 'action', action.type );

        default:
            return state;
    }
}

const reducer = combineReducers({
    BOOK, USER, ASIDE, MESSAGE
});

const store = applyMiddleware(
    regulateBookSearchResult,
    logOut,
    isLike
)( createStore )( reducer );

export default store;